/*
 * @(#)IAsciiDoctor.java 1.0 2021年02月04日
 *
 *  Copyright (c) 2016, YUNXI. All rights reserved.
 *  YUNXI PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package com.jimmy.utils.swagger2markup.asciidoctor;

import com.jimmy.utils.swagger2markup.AsciidoctorHelper;
import org.asciidoctor.*;

import static org.asciidoctor.Asciidoctor.Factory.create;

import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>Title:IAsciiDoctor</p>
 * <p>Description: </p>
 *
 * @author 子曰
 * @date 2021-2-4 15:29
 * @since 0.1.0
 */
public class HtmlAsciiDoctor implements IAsciiDoctor{
    //创建Asciidoctor
    private Asciidoctor asciidoctor;
    //文档属性
    private static Map<String, Object> attributes = new HashMap<String, Object>();

    static {
        attributes.put("doctype", "book");
        attributes.put("toc", "left");//左侧
        attributes.put("toclevels", "3");//三级菜单
        attributes.put("sectnums", true);//自动打数字序号
    }

    public HtmlAsciiDoctor() {
        this.asciidoctor = create();
    }

    @Override
    public String asciiDocToHtml(String filePath) {
        return null;
    }

    @Override
    public List<String> asciiDocsToHtml(List<String> filePathList) {
        return null;
    }

    @Override
    public List<String> asciiDocsToHtml(String directoryPath) {
        // 然后，需要将这些属性绑定到上文中的attributesBuilder
        AttributesBuilder attributesBuilder = AttributesBuilder.attributes();
        setAttributesOnBuilder(attributesBuilder);
        AsciidoctorHelper.addAttributes(attributes, attributesBuilder);

        OptionsBuilder options = OptionsBuilder.options();
        setOptionsOnBuilder(options);
        options.attributes(attributesBuilder);

        //文件夹下文件转换
        String[] result = asciidoctor.convertDirectory(
                new AsciiDocDirectoryWalker(directoryPath),
                options);
        return Arrays.asList(result);
    }

    /**
     * 可选builder
     * @param optionsBuilder
     */
    private static void setOptionsOnBuilder(OptionsBuilder optionsBuilder) {
        optionsBuilder
                .backend("html")//生成HTML
                .safe(SafeMode.UNSAFE)
                .headerFooter(true)
                .mkDirs(true)
                .docType("book")
                .toDir(new File(SysConstant.OUTPUT_HTML_DIR))//输出路径
                .destinationDir(new File(SysConstant.OUTPUT_HTML_DIR));//目标路径
    }

    /**
     * 可选属性
     * @param attributesBuilder
     */
    private static void setAttributesOnBuilder(AttributesBuilder attributesBuilder) {
        attributesBuilder.sourceHighlighter("coderay")
                .imagesDir("images@")
                .attributeMissing("skip")
                .attributeUndefined("drop-line");
    }
}
